/*
 * Copyright (c) 2023-2025 elsfs Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.elsfs.cloud.starter.excel.configuration;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.elsfs.cloud.starter.excel.aop.DynamicNameAspect;
import org.elsfs.cloud.starter.excel.aop.ResponseExcelReturnValueHandler;
import org.elsfs.cloud.starter.excel.kit.SpringContextKit;
import org.elsfs.cloud.starter.excel.processor.NameProcessor;
import org.elsfs.cloud.starter.excel.processor.NameSpelExpressionProcessor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

// import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

/** 配置初始化 */
@AutoConfiguration
@RequiredArgsConstructor
@Import({ExcelHandlerConfiguration.class, SpringContextKit.class})
@EnableConfigurationProperties(ExcelConfigProperties.class)
public class ExcelAutoConfiguration {

  private final ResponseExcelReturnValueHandler responseExcelReturnValueHandler;

  /**
   * SPEL 解析处理器
   *
   * @return NameProcessor excel名称解析器
   */
  @Bean
  @ConditionalOnMissingBean
  public NameProcessor nameProcessor() {
    return new NameSpelExpressionProcessor();
  }

  /**
   * Excel名称解析处理切面
   *
   * @param nameProcessor SPEL 解析处理器
   * @return DynamicNameAspect
   */
  @Bean
  @ConditionalOnMissingBean
  public DynamicNameAspect dynamicNameAspect(NameProcessor nameProcessor) {
    return new DynamicNameAspect(nameProcessor);
  }

  /** 追加 Excel返回值处理器 到 springmvc 中 */
  @PostConstruct
  public void setReturnValueHandlers() {
    //    List<HandlerMethodReturnValueHandler> returnValueHandlers = requestMappingHandlerAdapter
    //      .getReturnValueHandlers();
    //
    //    List<HandlerMethodReturnValueHandler> newHandlers = new ArrayList<>();
    //    newHandlers.add(responseExcelReturnValueHandler);
    //    assert returnValueHandlers != null;
    //    newHandlers.addAll(returnValueHandlers);
    //    requestMappingHandlerAdapter.setReturnValueHandlers(newHandlers);
  }

  /** 追加 Excel 请求处理器 到 springmvc 中 */
  @PostConstruct
  public void setRequestExcelArgumentResolver() {
    //    List<HandlerMethodArgumentResolver> argumentResolvers =
    // requestMappingHandlerAdapter.getArgumentResolvers();
    //    List<HandlerMethodArgumentResolver> resolverList = new ArrayList<>();
    //    resolverList.add(new RequestExcelArgumentResolver());
    //    resolverList.addAll(argumentResolvers);
    //    requestMappingHandlerAdapter.setArgumentResolvers(resolverList);
  }
}
